package com.rtmp.sample;

import com.rtmp.librtmp.RtmpUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * 测试前，请先设置HOST字段值
 */
public class RtmpTest {

    //此处需要替换为你自己的RTMP服务器地址
    private static final String HOST = "";

    private RtmpUtil rtmpUtil;

    @Before
    public void create() {
        rtmpUtil = new RtmpUtil();
    }

    @Test
    public void openFailTest() {
        boolean exceptionCheck = false;
        try {
            rtmpUtil.open("rtmp://dummy/vod", false);
        } catch (RtmpUtil.RtmpIOException e) {
            e.printStackTrace();
            exceptionCheck = true;
        }
        assertTrue(exceptionCheck);
    }

    @Test
    public void openSuccessTest() {
        boolean exceptionCheck = false;
        try {
            rtmpUtil.open(HOST, false);
        } catch (RtmpUtil.RtmpIOException e) {
            e.printStackTrace();
            exceptionCheck = true;
        }
        assertFalse(exceptionCheck);
    }

    @Test
    public void setTimeoutTest() {
        rtmpUtil.setTimeout(30 * 1000);
        boolean exceptionCheck = false;
        try {
            rtmpUtil.open(HOST, false);
        } catch (RtmpUtil.RtmpIOException e) {
            e.printStackTrace();
            exceptionCheck = true;
        }
        assertFalse(exceptionCheck);
    }

    @Test
    public void isConnectTest() throws RtmpUtil.RtmpIOException {
        rtmpUtil.open(HOST, false);
        boolean isConnect = rtmpUtil.isConnected();
        assertTrue(isConnect);
    }

    @Test
    public void readTest() throws RtmpUtil.RtmpIOException {
        rtmpUtil.open(HOST, false);
        byte[] bytes = new byte[2048];
        int result = rtmpUtil.read(bytes, 0, bytes.length);
        assertTrue(result >= 0);
    }

    @Test
    public void writeTest() throws RtmpUtil.RtmpIOException {
        rtmpUtil.open(HOST, true);
        byte[] bytes = new byte[]{70, 76, 86, 1, 1, 0, 0, 0, 9, 0, 0, 0, 0, 18, 0, 1, 88, 0, 0, 0, 0, 0, 0, 0, 2, 0, 10, 111, 110, 77, 101, 116, 97, 68, 97, 116, 97, 8, 0, 0, 0, 14, 0, 8, 100, 117, 114, 97, 116, 105, 111, 110, 0, 64, 78, 42, -97, -66, 118, -56, -76, 0, 5, 119, 105, 100, 116, 104, 0, 64, -122, -128, 0, 0, 0, 0, 0, 0, 6, 104, 101, 105, 103, 104, 116, 0, 64, -108, 0, 0, 0, 0, 0, 0, 0, 13, 118, 105, 100, 101, 111, 100, 97, 116, 97, 114, 97, 116, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 102, 114, 97, 109, 101, 114, 97, 116, 101, 0, 64, 62, 0, 0, 0, 0, 0, 0, 0, 12, 118, 105, 100, 101, 111, 99, 111, 100, 101, 99, 105, 100, 0, 64, 28, 0, 0, 0, 0, 0, 0, 0, 11, 109, 97, 106, 111, 114, 95, 98, 114, 97, 110, 100, 2, 0, 4, 109, 112, 52, 50, 0, 13, 109, 105, 110, 111, 114, 95, 118, 101, 114, 115, 105, 111, 110, 2, 0, 1, 48, 0, 17, 99, 111, 109, 112, 97, 116, 105, 98, 108, 101, 95, 98, 114, 97, 110, 100, 115, 2, 0, 8, 105, 115, 111, 109, 109, 112, 52, 50, 0, 17, 99, 111, 109, 46, 97, 110, 100, 114, 111, 105, 100, 46, 109, 111, 100, 101, 108, 2, 0, 8, 80, 105, 120, 101, 108, 32, 88, 76, 0, 19, 99, 111, 109, 46, 97, 110, 100, 114, 111, 105, 100, 46, 118, 101, 114, 115, 105, 111, 110, 2, 0, 2, 49, 48, 0, 24, 99, 111, 109, 46, 97, 110, 100, 114, 111, 105, 100, 46, 109, 97, 110, 117, 102, 97, 99, 116, 117, 114, 101, 114, 2, 0, 6, 71, 111, 111, 103, 108, 101, 0, 7, 101, 110, 99, 111, 100, 101, 114, 2, 0, 13, 76, 97, 118, 102, 53, 56, 46, 52, 53, 46, 49, 48, 48, 0, 8, 102, 105, 108, 101, 115, 105, 122, 101, 0, 65, 106, 111, -21, -96, 0, 0, 0, 0, 0, 9, 0, 0, 1, 99, 9, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 1, 100, 0, 31, -1, -31, 0, 25, 103, 100, 0, 31, -84, -39, 64, -76, 10, 27, 1, 108, -128, 0, 0, 3, 0, -128, 0, 0, 30, 7, -116, 24, -53, 1, 0, 6, 104, -21, -30, -53, 34, -64, -3, -8, -8, 0, 0, 0, 0, 62, 9, 0, 103, 115, 0, 0, 0, 0, 0, 0, 0, 23, 1, 0, 0, 67, 0, 0, 2, -95, 6, 5, -1, -1, -99, -36, 69, -23, -67, -26, -39, 72, -73, -106, 44, -40, 32, -39, 35, -18, -17, 120, 50, 54, 52, 32, 45, 32, 99, 111, 114, 101, 32, 49, 54, 49, 32, 45, 32, 72, 46, 50, 54, 52, 47, 77, 80, 69, 71, 45, 52, 32, 65, 86};
        int result = rtmpUtil.write(bytes);
        assertTrue(result > 0);
    }

    @Test
    public void pauseTest() throws RtmpUtil.RtmpIOException {
        rtmpUtil.open(HOST, false);
        byte[] bytes = new byte[2048];
        int result = rtmpUtil.read(bytes, 0, bytes.length);
        if (result < 0) {
            fail();
            return;
        }
        assertTrue(rtmpUtil.pause(true));
    }

    @Test
    public void playTest() throws RtmpUtil.RtmpIOException {
        rtmpUtil.open(HOST, false);
        byte[] bytes = new byte[2048];
        int result = rtmpUtil.read(bytes, 0, bytes.length);
        if (result < 0) {
            fail();
            return;
        }
        boolean pause = rtmpUtil.pause(true);
        if (!pause) {
            fail();
            return;
        }
        assertTrue(rtmpUtil.pause(false));
    }

    @After
    public void close() {
        if (rtmpUtil != null) {
            rtmpUtil.close();
        }
    }
}
